package com.eco.fishway.controller;


import com.eco.common.response.BaseResponse;
import com.eco.common.response.ResultTool;
import com.eco.fishway.service.LightService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "灯光控制接口")
@RestController
@RequestMapping("/ir/light")
@Slf4j
public class LightController {

    private static final String ON_COMMAND_LIGHT = "00 00 00 00 00 06 FF 05 00 66 FF 00";
    private static final String OFF_COMMAND_LIGHT = "00 00 00 00 00 06 FF 05 00 66 00 00";

    private final StringRedisTemplate stringRedisTemplate;

    private final LightService lightService;

    public LightController(LightService lightService, StringRedisTemplate stringRedisTemplate) {
        this.lightService = lightService;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @ApiOperation("获取开关状态")
    @GetMapping("status")
    public BaseResponse<Boolean> light(String id) {
        String status = stringRedisTemplate.opsForValue().get(id);
        return ResultTool.success("1".equals(status));
    }

    @ApiOperation("远程控制")
    @GetMapping("remoteControl")
    public BaseResponse<String> remoteControl(String id, String command) {
        if ("turnon".equals(command)) {
            command = ON_COMMAND_LIGHT;
        } else {
            command = OFF_COMMAND_LIGHT;
        }
        lightService.sendCommand(id, command);
        return ResultTool.success("开关成功");
    }


}